﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: Class.cs
#endregion

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.finan
{
	/// <summary>
	/// Description of Group.
	/// </summary>
	public class Class : DefaultClass, IClass
	{
		//private String   _id;
		private String   _code;
		private String   _name;
		private DateTime _register_date;
		private bool     _active;
		private String   _subgroup_id;
		
		public Class()
		{
			gsql.table= "finan.class";
			_sequence = "finan.class_class_id_seq";
		}
		
		public String code
		{
			set { _code = value; }
			get { return  _code; }
		}
		
		public String name
		{
			set { _name = value; }
			get { return  _name; }
		}
		
		public DateTime registerDate
		{
			set { _register_date = value; }
			get { return  _register_date; }
		}
		
		public bool active
		{
			set { _active = value; }
			get { return  _active; }
		}
		
		public String subGroupId
		{
			set { _subgroup_id = value; }
			get { return  _subgroup_id; }
		}
		
		public void clear()
		{
			_id 				= String.Empty;
			_code 			= String.Empty;
			_name				= String.Empty;
			_register_date = DateTime.Now;
			_active     	= true;
			_subgroup_id 	= String.Empty;
		}
	
	
		/// <summary>
		/// Fill the class with data of DataRow
		/// </summary>
		/// <param name="dr">DataRow with the data to fill the class.</param>				
		public override void get(DataRow dr)
		{
			_id       		= dr["class_id"].ToString();
			_code     		= dr["class_code"].ToString();
			_name     		= dr["class_name"].ToString();
			_register_date = Convert.ToDateTime(dr["class_register_date"]);
			_active   		= Convert.ToBoolean(dr["class_active"]);  
			_subgroup_id      = dr["class_subgroup_id"].ToString();
		}
	
		
		public override void setGsql()
		{
			gsql.setValue(_id, "class_id");
			gsql.setValue(_code, "class_code");
			gsql.setValue(_name, "class_name");
			//gsql.setValue(_register_date.ToString("dd/MM/yyyy"), "group_register_date");
			gsql.setValue(_active.ToString(), "class_active");
			gsql.setValue(_subgroup_id, "class_subgroup_id");
		}
	}


	//List
	public class Classs : DefaultList, IList
	{
		public Classs()
		{
			gsql.table= "finan.v_class";
		}		
		
		public Class get(Int32 index)
		{
			return (Class)items[index];
		}
		
		public Class get()
		{
			return (Class)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY class_code";
			
			return mountList();
		}
		
		public bool fillList(String subGroupId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "class_subgroup_id = "+subGroupId;
			gsql.additionalWhere = "ORDER BY class_code";
			
			return mountList();
		}		
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			clear();
			for (int i=0; i < tam; i++)
			{
				Class g = new Class();
				g.get(gsql.getRow(i));
				items.Add(g);
			}
			return true;
		}
		
	
		public void filterBySubGroup(string subGroupId)
		{
			int tam = items.Count;
			int i;
			
			filtered.Clear();
			for(i=0; i < tam; i++)
			{
				if (((Class)items[i]).subGroupId.Equals(subGroupId))
				{
					filtered.Add(items[i]);
				}
			}
			items = filtered;
		}	
	}
}

